﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Data;
using System.Windows;

namespace StarryEdge.WPF.Lang
{
    public class LangProvider : INotifyPropertyChanged
    {
        #region private
        private string _path;
        #endregion
        
        public LangProvider(string path)
        {
            _path = path;

        }
        private string _lang = "zh-cn";
        public string Lang => _lang;

        public void SetLang(string lang)
        {
            string langpath = Path.Combine(_path, $"{lang}.lang");
            if (File.Exists(langpath))
            {
                string[] strs = File.ReadAllLines(langpath);
                foreach (var item in strs)
                {
                    string[] keys = item.Split('=');
                    if (_keyDirc.ContainsKey(keys[0]))
                    {
                        _keyDirc[keys[0]] = keys[1];
                    }
                    else
                    {
                        _keyDirc.Add(keys[0], keys[1]);
                    }
                }
            }
            else
            {
                foreach (var item in _keyDirc)
                {
                    _keyDirc[item.Key] = "Null";
                }
            }
            OnPropertyChanged(Binding.IndexerName);
        }

        public void SaveKey()
        {
            StringBuilder stringBuilder = new StringBuilder();
            foreach (var item in _keyDirc)
            {
                stringBuilder.AppendLine($"{item.Key}={item.Value}");
            }

            if (!Directory.Exists(_path))
            {
                Directory.CreateDirectory(_path);
            }
            string file = Path.Combine(Environment.CurrentDirectory, "lang", "cn.lang");
            File.WriteAllText(file, stringBuilder.ToString());
        }

        public void BindingLang(DependencyObject dependencyObject, DependencyProperty dependencyProperty, string key) =>
            BindingOperations.SetBinding(dependencyObject, dependencyProperty, new Binding($"[{key}]")
            {
                Source = this,
                Mode = BindingMode.OneWay
            });

        private Dictionary<string, string> _keyDirc = new Dictionary<string, string>();

        [IndexerName("Item")]
        public string this[string name]
        {
            get
            {
                if (string.IsNullOrEmpty(name))
                {
                    return "Null";
                }
                if (_keyDirc.ContainsKey(name))
                {
                    return _keyDirc[name];
                }
                _keyDirc.Add(name, "Null");
                SaveKey();
                return "Null";
            }
        }

        #region INotifyPropertyChanged

        public event PropertyChangedEventHandler? PropertyChanged;

        protected virtual void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        #endregion INotifyPropertyChanged
    }
}
